package com.tuvi.nhamthin.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import com.tuvi.nhamthin.activity.Active;
import com.tuvi.nhamthin.helper.SoapHelper;

public class TuviApi implements ITuvi{
	public static final String NAMESPACE = "http://pitayasoftware.com/";
	public static final String URL = "http://tuvi.pitayasoftware.com/service/tuvi_nhamthin.asmx";
	public static final String TEST_METHOD = "test";
	public static final String GETMESSAGE_METHOD = "GetMessage";
	public static final String ACTIVECODE_METHOD = "validate_active_code";
	public static final String ADDNEW_USER_METHOD = "InserNewUserUsed";
	public static final String APP_ID = "9";
	public static final String USERNAME = "tuvi2012_android";
	public static final String PASSWORD = "zehIOsefG4oggFXAmA";
	public static final String APPID ="9";
	@Override
	public String getMessage(String userName, String password, String appId,
			String language) {
		// TODO Auto-generated method stub
		SoapHelper soapHelper = new SoapHelper();
		Map params = new HashMap<String, String>();
		params.put("userName", userName);
		params.put("passWord", password);
		params.put("appId", appId);
		params.put("LocationLanguage", language);
		try {
			SoapObject soapObj = soapHelper.getSoapData(TuviApi.NAMESPACE,
					TuviApi.GETMESSAGE_METHOD, 
					TuviApi.NAMESPACE +TuviApi.GETMESSAGE_METHOD,
					TuviApi.URL, params);
			return soapObj.getProperty(0).toString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean validateActiveCode(String userName, String password,
			String appId, String keycode, String uid) {
		// TODO Auto-generated method stub
		SoapHelper soapHelper = new SoapHelper();
		Map params = new HashMap<String, String>();
		params.put("UserName", userName);
		params.put("Password", password);
		params.put("AppID", appId);
		params.put("KeyCode", keycode.toUpperCase());
		params.put("UID", uid);
		try {
			SoapObject soapObj = soapHelper.getSoapData(TuviApi.NAMESPACE,
					TuviApi.ACTIVECODE_METHOD, 
					TuviApi.NAMESPACE +TuviApi.ACTIVECODE_METHOD,
					TuviApi.URL, params);
			String result = soapObj.getProperty(0).toString();//validate_active_codeResult
			if (result.contains("OK")) {
				Active.errorMessage = null;
				return true;
			}else{
				Active.errorMessage = result;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Active.errorMessage = null;
			e.printStackTrace();
			return false;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			Active.errorMessage = null;
			e.printStackTrace();
			return false;
		}
		return false;
	}

	@Override
	public boolean insertNewUserUsed(String userName, String password,
			String appID, String userDevice, String userTokenDevice,
			String deviceName, String model, String iosVersion,
			String location, String language) {
		// TODO Auto-generated method stub
		SoapHelper soapHelper = new SoapHelper();
		Map params = new HashMap<String, String>();
		params.put("UserName", "tuvi12congiap1");
		params.put("Password", "123456");
		params.put("AppID", "9");
		params.put("user_device", userDevice);
		params.put("user_token_device", userTokenDevice);
		params.put("device_name", deviceName);
		params.put("model", model);
		params.put("iosVersion", iosVersion);
		params.put("location", location);
		params.put("language", "en");
		try {
			SoapObject soapObj = soapHelper.getSoapData(TuviApi.NAMESPACE,
					TuviApi.ADDNEW_USER_METHOD, 
					TuviApi.NAMESPACE +TuviApi.ADDNEW_USER_METHOD,
					TuviApi.URL, params);
			String result = soapObj.getProperty(0).toString();//InserNewUserUsedResult
			if (result !="[MSG] ERROR") {
				return true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public String hoanHoChuBoDoiDanhMyTaiGhe(String iAppId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String test(String strval) {
		// TODO Auto-generated method stub
		return null;
	}

}
